package com.edais.actions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.edais.domain.BannerConfigTab;
import com.edais.domain.PushMessageRecord;
import com.edais.service.ActivityService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;
import com.edais.util.StringUtil;
import com.yunzhi.domain.RequestHeader;
import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultConstant;
import com.yunzhi.domain.ResultDTO;
import com.yunzhi.util.RequestUtil;
import com.yunzhi.util.ResponseUtil;

/**
 * @author taozun 2014-11-13 上午11:52:10 活动相关action
 * @modify huangyl	2016-09-18  新增banner2查询接口
 */
@Controller
@RequestMapping("/activity")

public class ActivityAction  implements Serializable{

	private static final long serialVersionUID = 1L;

	private static final Log logger = LogFactory.getLog(ActivityAction.class);

	@Autowired
	private ActivityService activityService;
	
	@RequestMapping("/getAppVersionList")	
	public void getAppVersionInfoList(HttpServletRequest request, HttpServletResponse response){

		if (!RequestUtil.validSign(request)) {
			logger.info("无效签名");
			ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
			return;
		}

		try{
			// 解密请求参数

			Map<String,Object> paramsMap = new HashMap<String, Object>();
			try {
				paramsMap = RequestUtil.decodeData(request);
			} catch (Exception e) {
				ResponseUtil.write(response, new ResultDTO(ResultCode.FAILED,"解析参数异常"));
				logger.info("解析参数异常");
				return;
			}
			logger.info("data：" + paramsMap.toString());

			String appType = (String) paramsMap.get("appType");
			if (StringUtils.isBlank(appType)) {
				ResultDTO resultDTO=new ResultDTO(ResultCode.FAILED,"app类型不能为空！");
				ResponseUtil.write(response,resultDTO);
				return;
			}

			/**
			 * 转换分页参数
			 */
			Map<String, String> requestParams = new HashMap<String, String>();
			int currentPage = Integer.parseInt(String.valueOf(paramsMap.get("current")));
			int pageSize = Integer.parseInt(String.valueOf(paramsMap.get("pageSize")));
			requestParams.put("rowBegin", (currentPage - 1) * pageSize + 1 + "");
			requestParams.put("rowEnd", currentPage * pageSize + "");
			requestParams.put("appType", appType);
			ResultDTO resultDTO = activityService.getAppVersionInfo(requestParams);

			logger.info("-----------------------------/activity/getAppVersionList ActivityAction getAppVersionInfoList() ---------------------------");

			ResponseUtil.write(response,resultDTO);

		} catch (Exception e) {
			logger.info("/activity/getAppVersionList出现异常-----" + e.getMessage());
			ResponseUtil.write(response,new ResultDTO(ResultCode.FAILED,Message.CnDescriptionExpression.BUSINESSE_ERROR));
			return;
		}
	}


	@RequestMapping(value = "/getBannerList")
	public void getBannerList(HttpServletRequest request, HttpServletResponse response) {
		logger.info("/activity/getBannerList ActivityAction getBannerList() start ");
		if (!RequestUtil.validSign(request)) {
			logger.info("无效签名");
			ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
			return;
		}
		RequestHeader header = RequestUtil.decodeHeader(request);
		logger.info("header：" + header.toString());
		Map<String,Object> params=null;
		try {
			params = RequestUtil.decodeData(request);	// 解密请求参数
		} catch (Exception e) {
			logger.error(" 解密请求参数失败：" ,e);
			ResultDTO<PushMessageRecord>  resultDTO = new ResultDTO<PushMessageRecord>(ResultCode.FAILED,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			String result  = resultDTO.toJsonString();
			ResponseUtil.write(response,  result);
			return;
		}
		logger.info("data：" + params.toString());
		String assortType = (String)params.get("assortType");		//分类标识
		String enable = (String)params.get("enable");               //理财是否启用（Y/N）商城是否启用（T/F）投资理财列表是否启用（R/W）活动推荐是否启用（U/D）积分banner是否显示（S/H）
		String showMedium=(String)params.get("showMedium");	        //显示（MOBILE:手机、WEB:网站、BOTH:手机和网站）
		if(StringUtil.isEmpty(assortType)){
			logger.info("assortType分类标识不能为空！");
			ResultDTO<PushMessageRecord>  resultDTO = new ResultDTO<PushMessageRecord>(ResultCode.FAILED,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			String result  = resultDTO.toJsonString();
			ResponseUtil.write(response,  result);
			return;
			
		}
		if(StringUtil.isEmpty(enable)){
			logger.info("enable可用标识不能为空！");
			ResultDTO<PushMessageRecord>  resultDTO = new ResultDTO<PushMessageRecord>(ResultCode.FAILED,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			String result  = resultDTO.toJsonString();
			ResponseUtil.write(response,  result);
			return;
			
		}
		if(StringUtil.isEmpty(showMedium)){
			logger.info("showMedium显示标识不能为空！");
			ResultDTO<PushMessageRecord>  resultDTO = new ResultDTO<PushMessageRecord>(ResultCode.FAILED,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			String result  = resultDTO.toJsonString();
			ResponseUtil.write(response,  result);
			return;
			
		}
		String[] showMediumArray=showMedium.split("_");
        List<String> showMediumList=new ArrayList<String>();		
		if (showMediumArray!=null&&showMediumArray.length>0) {
			for (int i = 0; i < showMediumArray.length; i++) {
				showMediumList.add(showMediumArray[i].toUpperCase());
			}
		}
		params.put("list", showMediumList);
		List<BannerConfigTab> resultList=null;
		try {
			resultList = activityService.getBannerList(params);
		} catch (Exception e) {
			logger.error("/activity/getBannerList ActivityAction getBannerList()  Banner图列表查询出现异常",e);
			ResultDTO<PushMessageRecord>  resultDTO = new ResultDTO<PushMessageRecord>(ResultCode.FAILED,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			String result  = resultDTO.toJsonString();
			ResponseUtil.write(response,  result);
			return;
		}
		ResultDTO<Map<String, Object>> resultDTO = new ResultDTO<Map<String, Object>>(ResultCode.SUCCESS);
		JSONObject jsonObj=resultDTO.toJson();
		jsonObj.put("list", resultList);
		if (resultList!=null&&resultList.size()>0) {
			jsonObj.put("totalNum", resultList.size());
		}
		ResponseUtil.write(response, jsonObj.toJSONString());
		logger.info("/activity/getBannerList ActivityAction getBannerList() end ");
	}
	
	/**
	 * 活动列表查询
	 * @throws Exception 
	 * @author gongyanlan
	 * @date 2017-2-21
	 */
	@RequestMapping(value = "/getAcList")
	public void getAcList(HttpServletRequest request, HttpServletResponse response){
		logger.info("/activity/getAcList ActivityAction getAcList() start ");
		if (!RequestUtil.validSign(request)) {
			logger.info("无效签名");
			ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
			return;
		}
		RequestHeader header = RequestUtil.decodeHeader(request);
		logger.info("header：" + header.toString());
		Map<String,Object> params=null;
		try {
			params = RequestUtil.decodeData(request);	// 解密请求参数
		} catch (Exception e) {
			logger.error(" 解密请求参数失败：" ,e);
			ResultDTO<PushMessageRecord>  resultDTO = new ResultDTO<PushMessageRecord>(ResultCode.FAILED,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			String result  = resultDTO.toJsonString();
			ResponseUtil.write(response,  result);
			return;
		}
		logger.info("data：" + params.toString());
		//当前页数
		String currentStr = String.valueOf(params.get("current"));		
		//页面大小
		String pageSizeStr = String.valueOf(params.get("pageSize"));
		logger.info("/activity/getAcList ActivityAction getAcList()  current=" + currentStr + " , pageSize=" + pageSizeStr);
		if(StringUtil.isEmpty(currentStr)){
			//设置默认值为1
			currentStr = "1";
			logger.info("/activity/getAcList ActivityAction getAcList() current=" + currentStr + " , current为空!");
		}
		if(StringUtil.isEmpty(pageSizeStr)){
			//设置默认值为10
			pageSizeStr = "10";
			logger.info("/activity/getAcList ActivityAction getAcList()  pageSize=" + pageSizeStr + " , pageSize为空!");
		}
		int current = Integer.parseInt(currentStr);
		int pageSize = Integer.parseInt(pageSizeStr);
		//重新组装参数	
		params.put("current", current);
		params.put("pageSize", pageSize);
		ResultDTO<BannerConfigTab> rstDTO=null;
		try {
			rstDTO = activityService.getAcList(params);
		} catch (Exception e) {
			logger.error("/activity/getAcList ActivityAction getAcList()  活动列表查询出现异常",e);
			ResultDTO<PushMessageRecord>  resultDTO = new ResultDTO<PushMessageRecord>(ResultCode.FAILED,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			String result  = resultDTO.toJsonString();
			ResponseUtil.write(response,  result);
			return;
		}
		String result  = rstDTO.toJsonString();
		logger.info("/activity/getAcList ActivityAction getAcList() end ");
		ResponseUtil.write(response,  result);
	}
	
	@RequestMapping(value = "/getBannerListSEC")
	public void getBannerListSEC(HttpServletRequest request,
			HttpServletResponse response) {
		List bannerList = new ArrayList();
		try {
			bannerList = this.activityService.getBannerListSEC();
			logger.debug("/activity/getBannerListSEC bannerList.size():"
					+ bannerList.size());

			Map<String, Object> mapResult = new HashMap<String, Object>();
			mapResult.put("list", bannerList);
			mapResult.put("totalNum", bannerList.size());
			mapResult.put("listSize", bannerList.size());
			mapResult.put("resmsg_cn", "");
			String result = CommonUtil.setResultInfo(mapResult,
					Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE);
			// 处理返回结果
			CommonUtil.responseJson(result, response);

		} catch (Exception e) {
			logger.debug("/activity/getBannerListSEC异常-----" + e.getMessage());
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE,
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
		return;
	}
}
